﻿using Dapper;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using OMG.Fill.Mulberry.Model;
using OMG.Fill.Mulberry.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace OMG.Fill.Mulberry.Service
{
    /// <summary>
    /// 表3全年蚕桑生产情况统计表T_MulAnnualProducStatist通用操作类
    /// </summary>
    public class MulAnnualProducStatistServices : MulAnnualProducStatists
    {
        ReportServices rs = new ReportServices();
        public Pager<List<MulAnnualProducStatistEntity>> GetPageList(MulAnnualProducStatistQuery query)
        {
            Pager<List<MulAnnualProducStatistEntity>> obj = new Pager<List<MulAnnualProducStatistEntity>>();
            var paramlist = new DynamicParameters();
            string strWhereSql = "";//from 表在下面，需完善其他查询条件在这里接的写，格式为："and xxx=@xxx空格,如下注释部分代码所示："
            //if (!string.IsNullOrEmpty(query.GroupCode))
            //{
            //  strWhereSql += "and GroupCode = @GroupCode ";
            //  paramlist.Add("GroupCode", query.GroupCode);
            //}
            string strFromSql = "from dbo.T_MulAnnualProducStatist where 1=1 " + strWhereSql;
            string strTotal = "select count(1)" + strFromSql + strWhereSql;
            string strOrderSql = "";//自定义完成order by语句
            string strSql = @"select * from (select *,ROW_NUMBER() over (order by Id ) as RowNumber " + strFromSql + ") as t ";
            strSql += "where t.RowNumber between  @minRow and @maxRow" + strOrderSql;
            paramlist.Add("minRow", query.MinRow);
            paramlist.Add("maxRow", query.MaxRow);
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.rows = dbCon.Query<MulAnnualProducStatistEntity>(strSql, paramlist).ToList();
                    obj.total = dbCon.Query<int>(strTotal, paramlist).FirstOrDefault();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取表3全年蚕桑生产情况统计表T_MulAnnualProducStatist分页列表失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 获取指定对象
        /// </summary>
        public override ResResult<MulAnnualProducStatistEntity> GetModel(string ReportId)
        {
            ResResult<MulAnnualProducStatistEntity> obj = new ResResult<MulAnnualProducStatistEntity>();
            MulAnnualProducStatistEntity model = new MulAnnualProducStatistEntity();
            if (CheckRepCount(ReportId))
            {
                model.ReportId = ReportId;
                if (AddInfo(model).status > 0)
                {
                    obj.model = model;
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                }
            }
            else
            {
                string strSql = @"select * from dbo.T_MulAnnualProducStatist where ReportId = @ReportId";
                try
                {
                    using (var dbCon = ConnectFactory.GetOpenConnection())
                    {
                        obj.model = dbCon.Query<MulAnnualProducStatistEntity>(strSql, new { ReportId = ReportId }).FirstOrDefault();
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "获取成功！";
                        dbCon.Dispose();//数据库连接释放
                    }
                }
                catch (Exception ex)
                {
                    obj.status = DefaultKey.ErrorResult;
                    obj.message = ex.ToString().Trim();
                    LogHelper.ErrorLog("获取表1桑园面积统计表T_MulAnnualProducStatist对象信息失败", ex);

                }
            }
            return obj;
        }

        public ResResult<MulAnnualProducStatistModel> GetModel(string AreaCode, string ReportId, string FillInYear)
        {
            ResResult<MulAnnualProducStatistModel> obj = new ResResult<MulAnnualProducStatistModel>();
            MulAnnualProducStatistModel model = new MulAnnualProducStatistModel();
            model.IntoMulArea = 0;
            model.SeedSizeSpring = 0;
            model.SeedSizeSummer = 0;
            model.SeedSizeAutumn = 0;
            model.FCYSpring = 0;
            model.FCYSummer = 0;
            model.FCYAutumn = 0;
            model.AFCSpring = 0;
            model.AFCSummer = 0;
            model.AFCAutumn = 0;
            model.SRSSpring = 0;
            model.SRSSummer = 0;
            model.SRSAutumn = 0;
            model.SeedSizeTotal = 0;
            model.FCYTotal = 0;
            model.AFCTotal = 0;
            model.SRSTotal = 0;
            string strSql = @"select * from dbo.T_MulAnnualProducStatist where ReportId = @ReportId";
            string strArea = @"SELECT IntoMulArea,AreaCode,FillInYear FROM T_MulAreaStatist ms left JOIN T_Report r ON r.Id = ms.ReportId ";
            strArea += "where  ";
            //strArea += "  r.ReportCode = 'MulQuartProduc'"; //--报表代码
            strArea += "  FillInYear = @FillInYear "; //--填报年度
            strArea += "  and AreaCode =@AreaCode ";
            string AreaSql = @"select* from dbo.T_Report r right join dbo.T_MulQuartProduc mp on r.Id = mp.ReportId   ";
            AreaSql += "where  ";
            //AreaSql += "  r.ReportCode = 'MulQuartProduc'"; //--报表代码
            AreaSql += "  FillInYear = @FillInYear "; //--填报年度
            AreaSql += "  and AreaCode =@AreaCode ";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    var FirstModel = dbCon.Query<MulAnnualProducStatistModel>(strSql, new { ReportId = ReportId }).FirstOrDefault();
                    var mlm = dbCon.Query<MulQuartStatsModel>(AreaSql, new { AreaCode = AreaCode, FillInYear = FillInYear }).ToList();
                    var mArea= dbCon.Query<MulAreaStatsModel>(strArea, new { AreaCode = AreaCode, FillInYear = FillInYear }).FirstOrDefault();
                    //分隔
                    if (FirstModel != null)
                    {
                        model.RawSilkYield = FirstModel.RawSilkYield;
                        model.RawSilkValue = FirstModel.RawSilkValue;
                        model.SilkYield = FirstModel.SilkYield;
                        model.SilkValue = FirstModel.SilkValue;
                        model.SilkQuiltYield = FirstModel.SilkQuiltYield;
                        model.SilkQuiltValue = FirstModel.SilkQuiltValue;
                    }
                    if (mlm != null)
                    {
                        foreach (var m in mlm)
                        {
                            if (m.TimesCycle == "春季")
                            {
                                model.SeedSizeSpring = m.SeedSize;
                                model.FCYSpring = m.FreshCocoonYield;
                                model.AFCSpring = m.AveFreshCocoon;
                                model.SRSSpring = m.SmallRearSilk;
                            }
                            else if (m.TimesCycle == "夏季")
                            {
                                model.SeedSizeSummer = m.SeedSize;
                                model.FCYSummer = m.FreshCocoonYield;
                                model.AFCSummer = m.AveFreshCocoon;
                                model.SRSSummer = m.SmallRearSilk;
                            }
                            else if (m.TimesCycle == "正秋" )
                            {
                                model.SeedSizeAutumn += m.SeedSize;
                                model.FCYZAutumn = m.FreshCocoonYield == null ? 0 : m.FreshCocoonYield;//正秋产量
                                model.AFCZAutumn = m.AveFreshCocoon == null ? 0 : m.AveFreshCocoon; //正秋均价
                                model.SRSAutumn += m.SmallRearSilk;
                            }
                            else if (m.TimesCycle == "晚秋")
                            {
                                model.SeedSizeAutumn += m.SeedSize;
                                model.FCYWAutumn = m.FreshCocoonYield==null?0: m.FreshCocoonYield;//晚秋产量
                                model.AFCWAutumn = m.AveFreshCocoon == null ? 0 : m.AveFreshCocoon;//晚秋均价
                                model.SRSAutumn += m.SmallRearSilk;
                            }
                            
                            //model.IntoMulArea +=m.IntoMulArea;
                            model.SeedSizeTotal += m.SeedSize == null ? 0 : m.SeedSize;
                            model.FCYTotal += m.FreshCocoonYield == null ? 0 : m.FreshCocoonYield;
                            
                            model.SRSTotal += m.SmallRearSilk == null ? 0 : m.SmallRearSilk;
                        }
                        if(mArea !=null)
                        {
                            model.IntoMulArea = mArea.IntoMulArea;
                        }
                    }
                    model.FCYAutumn = model.FCYWAutumn + model.FCYZAutumn;
                    model.AFCAutumn =Convert.ToDecimal( Math.Round(Convert.ToDouble((model.AFCZAutumn * model.FCYZAutumn + model.AFCWAutumn * model.FCYWAutumn) / model.FCYAutumn), 2));
                    model.AFCTotal = ((model.AFCSpring * model.FCYSpring) + (model.AFCSummer * model.FCYSummer) + (model.AFCAutumn * model.FCYAutumn))/ model.FCYTotal;
                    obj.model = model;
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取表1桑园面积统计表T_MulAnnualProducStatist对象信息失败", ex);

            }
            return obj;
        }
        /// <summary>
        /// 判定是否生成过报表
        /// </summary>
        /// <param name="reportId"></param>
        /// <returns>没生成过true,否则false</returns>
        public bool CheckRepCount(string reportId)
        {
            string strSql = @"select count(1) from dbo.T_MulAnnualProducStatist where ReportId=@ReportId";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    //为0，没生成过true,否则false
                    return dbCon.Query<int>(strSql, new { ReportId = reportId }).FirstOrDefault() == 0 ? true : false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("判断是否生成过表1桑园面积统计表T_MulAnnualProducStatist失败", ex);
                return false;
            }
        }

        /// <summary>
        /// 获取今往年数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResResult<MulAnnViewModel> GetModel(MulNowModel model)
        {
            ResResult<MulAnnViewModel> obj = new ResResult<MulAnnViewModel>();
            MulAnnualProducStatistEntity vfEntity = new MulAnnualProducStatistEntity();
            MulAnnViewModel vvm = new MulAnnViewModel();
            DictServices dictServices = new DictServices();
            var paramlist = new DynamicParameters();
            string strSqlReport = "";
            if (model != null)
            {
                if (!string.IsNullOrEmpty(model.AreaCode) && !string.IsNullOrEmpty(model.ReportCode) && !string.IsNullOrEmpty(model.FillInYear))
                {
                    string FillInYear = dictServices.GetKey(model.FillInYear);
                    paramlist.Add("AreaCode", model.AreaCode);
                    paramlist.Add("ReportCode", model.ReportCode);
                    paramlist.Add("FillInYear", FillInYear);
                    int OldFillInYear = Convert.ToInt32(FillInYear) - 1;
                    if (OldFillInYear < 0)
                    {
                        OldFillInYear = 0;
                    }
                    paramlist.Add("OldFillInYear", OldFillInYear.ToString());
                    string strNowSql = "SELECT mp.Id,mp.ReportId,mp.RawSilkValue,mp.RawSilkYield,mp.SilkQuiltValue,mp.SilkQuiltYield,mp.SilkValue,mp.SilkYield,ReportCode,Status as ReportStatus "
                        + " FROM T_Report r left join  T_MulAnnualProducStatist mp on r.Id = mp.ReportId WHERE ReportCode = @ReportCode and FillInYear = @FillInYear and AreaCode = @AreaCode ";
                    string strOldSql = "SELECT mp.Id as OldId,mp.ReportId as OldReportId,mp.RawSilkValue as OldRawSilkValue,mp.RawSilkYield as OldRawSilkYield,mp.SilkQuiltValue as OldSilkQuiltValue,mp.SilkQuiltYield as OldSilkQuiltYield,mp.SilkValue as OldSilkValue,mp.SilkYield as OldSilkYield,ReportCode,Status as OldReportStatus "
                        + " FROM T_Report r left join  T_MulAnnualProducStatist mp on r.Id = mp.ReportId WHERE ReportCode = @ReportCode and FillInYear = @OldFillInYear and AreaCode = @AreaCode ";
                    strSqlReport = "SELECT * FROM ((" + strNowSql + ")t left JOIN  (" + strOldSql + ")tt ON t.ReportCode = tt.ReportCode)";

                    Boolean currentResult = rs.CheckRepCreate(model.ReportCode, model.AreaCode, FillInYear);
                    Boolean OldResult = rs.CheckRepCreate(model.ReportCode, model.AreaCode, OldFillInYear.ToString());
                    //判断选择的年度与上一年度是否生成过报表
                    if (currentResult && OldResult)
                    {
                        obj.model = vvm;
                        obj.status = -1;
                        obj.message = model.FillInYear + "与上一年的报表没有填报数据！！";
                    }
                    //判断本年是否生成过报表
                    else if (currentResult)
                    {
                        var dbCon = ConnectFactory.GetOpenConnection();
                        obj.model = dbCon.Query<MulAnnViewModel>(strOldSql, paramlist).FirstOrDefault();
                        obj.status = -2;
                        obj.message = model.FillInYear + "的报表没有填报数据！！";
                        dbCon.Dispose();//数据库连接释放
                    }
                    else
                    {
                        var dbCon = ConnectFactory.GetOpenConnection();
                        //获取报表Id
                        vfEntity.ReportId = rs.GetReportId(model.ReportCode, model.AreaCode, FillInYear);
                        //判断表是否初始化过
                        if (CheckRepCount(vfEntity.ReportId))
                        {
                            if (AddInfo(vfEntity).status > 0)//报表初始化
                            {
                                //查询去年数据
                                obj.model = dbCon.Query<MulAnnViewModel>(strOldSql, paramlist).FirstOrDefault();
                                obj.status = 1;//初始化成功status为1
                                obj.message = "MulAnnualProducStatist报表初始化成功！";
                                return obj;
                            }
                        }
                        else
                        {
                            try
                            {
                                obj.model = dbCon.Query<MulAnnViewModel>(strSqlReport, paramlist).FirstOrDefault();
                                obj.status = 2;//成功状态为2
                                obj.message = "获取MulAnnualProducStatist报表今往年数据成功！";
                                return obj;
                            }
                            catch (Exception ex)
                            {
                                obj.status = 0; //失败状态
                                obj.message = ex.ToString().Trim();
                                LogHelper.ErrorLog("获取MulAnnualProducStatist报表今往年数据失败", ex);
                            }
                        }
                    }
                }
            }
            return obj;
        }

        /// <summary>
        /// 读取导入的Excel数据
        /// </summary>
        /// <param name="pathName">导入文件路径</param>
        /// <param name="fileSuffix">文件后缀</param>
        /// <param name="sheetName">sheet名称</param>
        /// <param name="startRow">从第几行开始读取数据</param>
        /// <returns></returns>
        public List<MulAnnInfoModel> ExcelToModel(string pathName, string fileSuffix, string sheetName, int startRow)
        {

            IWorkbook workbook = null;
            ISheet sheet = null;
            List<MulAnnInfoModel> list = new List<MulAnnInfoModel>();
            FileStream fs = new FileStream(pathName, FileMode.Open, FileAccess.Read);
            switch (fileSuffix)
            {
                case ".xls":
                    workbook = new HSSFWorkbook(fs);
                    break;
                case ".xlsx":
                    workbook = new XSSFWorkbook(fs);
                    break;
                default:
                    workbook = new HSSFWorkbook(fs);
                    break;
            }
            if (!string.IsNullOrEmpty(sheetName))
            {
                sheet = workbook.GetSheet(sheetName);
                if (sheet == null) //如果没有找到指定的sheetName对应的sheet，则尝试获取第一个sheet
                {
                    sheet = workbook.GetSheetAt(0);
                }
            }
            else
            {
                sheet = workbook.GetSheetAt(0);
            }
            if (sheet != null)
            {
                //获取有数据的行数
                int rowCount = 0;
                for (int i = startRow; i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    if (row != null)
                    {
                        rowCount = sheet.GetRow(i).RowNum;
                    }
                    else
                    {
                        break;
                    }
                }
                for (int i = startRow; i <= rowCount; i++)
                {
                    IRow row = sheet.GetRow(i);
                    MulAnnInfoModel model = new MulAnnInfoModel();
                    //model.year = row.GetCell(0).ToString();
                    if (row.GetCell(0) != null && row.GetCell(0).ToString() != "")
                    {
                        model.RawSilkYield = decimal.Parse(row.GetCell(0).ToString());
                    }
                    else
                    {
                        model.RawSilkYield = null;
                    }
                    if (row.GetCell(1) != null && row.GetCell(1).ToString() != "")
                    {
                        model.RawSilkValue = decimal.Parse(row.GetCell(1).ToString());
                    }
                    else
                    {
                        model.RawSilkValue = null;
                    }
                    if (row.GetCell(2) != null && row.GetCell(2).ToString() != "")
                    {
                        model.SilkYield = decimal.Parse(row.GetCell(2).ToString());
                    }
                    else
                    {
                        model.SilkYield = null;
                    }
                    if (row.GetCell(3) != null && row.GetCell(3).ToString() != "")
                    {
                        model.SilkValue = decimal.Parse(row.GetCell(3).ToString());
                    }
                    else
                    {
                        model.SilkValue = null;
                    }
                    if (row.GetCell(4) != null && row.GetCell(4).ToString() != "")
                    {
                        model.SilkQuiltYield = decimal.Parse(row.GetCell(4).ToString());
                    }
                    else
                    {
                        model.SilkQuiltYield = null;
                    }
                    if (row.GetCell(5) != null && row.GetCell(5).ToString() != "")
                    {
                        model.SilkQuiltValue = decimal.Parse(row.GetCell(5).ToString());
                    }
                    else
                    {
                        model.SilkQuiltValue = null;
                    }
                    list.Add(model);
                }
            }
            return list;
        }

        /// <summary>
        /// 添加
        /// </summary>
        public virtual ResResult<int> AddInfo(MulAnnInfoModel model)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"INSERT INTO dbo.T_MulAnnualProducStatist";
            strSql += "(";
            strSql += "Id,";
            strSql += "ReportId,";
            strSql += "RawSilkYield,";
            strSql += "RawSilkValue,";
            strSql += "SilkYield,";
            strSql += "SilkValue,";
            strSql += "SilkQuiltYield,";
            strSql += "SilkQuiltValue)";
            strSql += " values(";
            strSql += "@Id,";
            strSql += "@ReportId,";
            strSql += "@RawSilkYield,";
            strSql += "@RawSilkValue,";
            strSql += "@SilkYield,";
            strSql += "@SilkValue,";
            strSql += "@SilkQuiltYield,";
            strSql += "@SilkQuiltValue)";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    model.Id = CustomFun.GetGuid();
                    int result = dbCon.Execute(strSql, model);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "添加成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "添加失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("新增表3全年蚕桑生产情况统计表T_MulAnnualProducStatist对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        public virtual ResResult<int> UpdateInfo(MulAnnInfoModel model)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"UPDATE dbo.T_MulAnnualProducStatist set ";
            strSql += "RawSilkYield = @RawSilkYield,";
            strSql += "RawSilkValue = @RawSilkValue,";
            strSql += "SilkYield = @SilkYield,";
            strSql += "SilkValue = @SilkValue,";
            strSql += "SilkQuiltYield = @SilkQuiltYield,";
            strSql += "SilkQuiltValue = @SilkQuiltValue";
            strSql += " where ReportId = @ReportId";
            using (var dbCon = ConnectFactory.GetOpenConnection())
            {
                var trans = dbCon.BeginTransaction();
                try
                {
                    int result = dbCon.Execute(strSql, model, trans);
                    string strRep = "update dbo.T_Report set Status='2',UpdateTime=@UpdateTime where Id=@ReportId";
                    dbCon.Execute(strRep, new { ReportId = model.ReportId, UpdateTime = DateTime.Now }, trans);
                    //提交事务
                    trans.Commit();
                    obj.model = result;
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "更新成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    obj.status = DefaultKey.ErrorResult;
                    obj.message = ex.ToString().Trim();
                    LogHelper.ErrorLog("更新表3全年蚕桑生产情况统计表T_MulAnnualProducStatist对象信息失败", ex);
                    return obj;
                }
            }


        }

        /// <summary>
        /// 根据reportId获取id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual ResResult<MulAreaStatistEntity> GetModelId(string ReportId)
        {
            ResResult<MulAreaStatistEntity> obj = new ResResult<MulAreaStatistEntity>();
            string strSql = @"select * from dbo.T_MulAnnualProducStatist where ReportId = @ReportId";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<MulAreaStatistEntity>(strSql, new { ReportId = ReportId }).FirstOrDefault();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取表T_MulAnnualProducStatist对象信息失败", ex);
                return obj;
            }
        }

        /// <summary>
        /// 饲养蚕品种构成统计--------(按年统计)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public ResResult<List<MulAnnStatsModel>> GetStatsList(MulNowModel model)
        {
            DictServices dictServices = new DictServices();
            if (!string.IsNullOrEmpty(model.FillInYear))
            {
                model.FillInYear = dictServices.GetPZKey(model.FillInYear, "ReportYear");
            }
            ResResult<List<MulAnnStatsModel>> result = new ResResult<List<MulAnnStatsModel>>();
            #region  /*县级统计代码*/
            string StrAreaSql = "select Area.Code AreaCode, Dict.DictText year, Area.FullName AreaName,isnull(IntoMulArea,0)IntoMulArea,"
                + "isnull(SpringSeedSize, 0)SpringSeedSize,isnull(SummerSeedSize, 0)SummerSeedSize,"
                + "isnull(FallSeedSize, 0)FallSeedSize,isnull(SpringFreshCocoonYield, 0)SpringFreshCocoonYield,"
                + "isnull(SummerFreshCocoonYield, 0)SummerFreshCocoonYield,isnull(FallFreshCocoonYield, 0)FallFreshCocoonYield,"
                + "isnull(SpringAveFreshCocoon, 0)SpringAveFreshCocoon,isnull(SummerAveFreshCocoon,0)SummerAveFreshCocoon,isnull(FallAveFreshCocoon, 0)FallAveFreshCocoon,"
                + "isnull(SpringSmallRearSilk, 0)SpringSmallRearSilk,isnull(SummerSmallRearSilk, 0)SummerSmallRearSilk,"
                + "isnull(FallSmallRearSilk, 0)FallSmallRearSilk,ISNULL(RawSilkYield, 0)RawSilkYield,ISNULL(RawSilkValue, 0)RawSilkValue,"
                + "ISNULL(SilkYield, 0)SilkYield,isnull(SilkValue,0)SilkValue,ISNULL(SilkQuiltYield, 0)SilkQuiltYield,ISNULL(SilkQuiltValue, 0)SilkQuiltValue from( "
                + "select case when grouping(Area.Code) = 1 then @AreaCode else Code end Code,"
                + "SUM(IntoMulArea)IntoMulArea,"
                + "SUM(CASE TimesCycle WHEN '春季' THEN SeedSize ELSE 0 END)SpringSeedSize,"
                + "SUM(CASE TimesCycle WHEN '夏季' THEN SeedSize ELSE 0 END)SummerSeedSize,"
                + "SUM((CASE TimesCycle WHEN '正秋' THEN SeedSize ELSE 0 END) + (CASE TimesCycle WHEN '晚秋' THEN SeedSize ELSE 0 END))FallSeedSize,"
                + "SUM(CASE TimesCycle WHEN '春季' THEN FreshCocoonYield ELSE 0 END)SpringFreshCocoonYield,"
                + "SUM(CASE TimesCycle WHEN '夏季' THEN FreshCocoonYield ELSE 0 END)SummerFreshCocoonYield,"
                + "SUM((CASE TimesCycle WHEN '正秋' THEN FreshCocoonYield ELSE 0 END) + (CASE TimesCycle WHEN '晚秋' THEN FreshCocoonYield ELSE 0 END))FallFreshCocoonYield,"
                + "SUM(CASE TimesCycle WHEN '春季' THEN AveFreshCocoon ELSE 0 END)SpringAveFreshCocoon,"
                + "SUM(CASE TimesCycle WHEN '夏季' THEN AveFreshCocoon ELSE 0 END)SummerAveFreshCocoon,"
                + "SUM(((CASE TimesCycle WHEN '正秋' THEN AveFreshCocoon ELSE 0 END)*(CASE TimesCycle WHEN '正秋' THEN FreshCocoonYield ELSE 0 END))+ ((CASE TimesCycle WHEN '晚秋' THEN AveFreshCocoon ELSE 0 END)*(CASE TimesCycle WHEN '晚秋' THEN FreshCocoonYield ELSE 0 END)))FallAveFreshCocoon,"
                + "SUM(CASE TimesCycle WHEN '春季' THEN SmallRearSilk ELSE 0 END)SpringSmallRearSilk,"
                + "SUM(CASE TimesCycle WHEN '夏季' THEN SmallRearSilk ELSE 0 END)SummerSmallRearSilk,"
                + "SUM((CASE TimesCycle WHEN '正秋' THEN SmallRearSilk ELSE 0 END) + (CASE TimesCycle WHEN '晚秋' THEN SmallRearSilk ELSE 0 END))FallSmallRearSilk,"
                + "SUM(RawSilkYield)RawSilkYield,SUM(RawSilkValue)RawSilkValue,SUM(SilkYield)SilkYield,SUM(SilkValue)SilkValue,"
                + "SUM(SilkQuiltYield)SilkQuiltYield,SUM(SilkQuiltValue)SilkQuiltValue "
                + "from([dbo].[T_Report] Report left join T_MulQuartProduc mc on Report.Id = mc.ReportId "
                + "left join T_MulAnnualProducStatist ma on ma.ReportId = Report.Id left join "
                + "T_MulAreaStatist mas on mas.ReportId = Report.Id)right join T_Area Area on Area.Code = Report.AreaCode "
                + "where Area.ParentCode = @AreaCode  and Report.Status = @ReportStatus and Report.FillInYear = @FillInYear "
                + "group by rollup(Area.Code)) tt1 left join T_Area Area on tt1.Code = Area.Code inner join T_Dict Dict on Dict.GroupCode = 'ReportYear' and DictValue = @FillInYear "
                + "where area.ParentCode = @AreaCode  or area.Code = @AreaCode order by AreaCode asc;";
            #endregion
            #region/*市级统计代码*/
            string StrCitySql = "select Area.Code AreaCode,Dict.DictText year, Area.FullName AreaName, isnull(IntoMulArea,0)IntoMulArea,"
                + "isnull(SpringSeedSize, 0)SpringSeedSize,isnull(SummerSeedSize, 0)SummerSeedSize,"
                +"isnull(FallSeedSize, 0)FallSeedSize,isnull(SpringFreshCocoonYield, 0)SpringFreshCocoonYield,"
                +"isnull(SummerFreshCocoonYield, 0)SummerFreshCocoonYield,isnull(FallFreshCocoonYield, 0)FallFreshCocoonYield,"
                + "isnull(SpringAveFreshCocoon, 0)SpringAveFreshCocoon,isnull(SummerAveFreshCocoon,0)SummerAveFreshCocoon,isnull(FallAveFreshCocoon, 0)FallAveFreshCocoon,"
                + "isnull(SpringSmallRearSilk, 0)SpringSmallRearSilk,isnull(SummerSmallRearSilk, 0)SummerSmallRearSilk,"
                +"isnull(FallSmallRearSilk, 0)FallSmallRearSilk,ISNULL(RawSilkYield, 0)RawSilkYield,ISNULL(RawSilkValue, 0)RawSilkValue,"
                +"ISNULL(SilkYield, 0)SilkYield,ISNULL(SilkQuiltYield, 0)SilkQuiltYield,ISNULL(SilkQuiltValue, 0)SilkQuiltValue "
                + "from(select case when grouping(Area.ParentCode) = 1 then @AreaCode else ParentCode end ParentCode,"
                + "SUM(IntoMulArea)IntoMulArea,"
                +"SUM(CASE TimesCycle WHEN '春季' THEN SeedSize ELSE 0 END)SpringSeedSize,"
                +"SUM(CASE TimesCycle WHEN '夏季' THEN SeedSize ELSE 0 END)SummerSeedSize,"
                +"SUM((CASE TimesCycle WHEN '正秋' THEN SeedSize ELSE 0 END) + (CASE TimesCycle WHEN '晚秋' THEN SeedSize ELSE 0 END))FallSeedSize,"
                +"SUM(CASE TimesCycle WHEN '春季' THEN FreshCocoonYield ELSE 0 END)SpringFreshCocoonYield,"
                +"SUM(CASE TimesCycle WHEN '夏季' THEN FreshCocoonYield ELSE 0 END)SummerFreshCocoonYield,"
                +"SUM((CASE TimesCycle WHEN '正秋' THEN FreshCocoonYield ELSE 0 END) + (CASE TimesCycle WHEN '晚秋' THEN FreshCocoonYield ELSE 0 END))FallFreshCocoonYield,"
                +"SUM(CASE TimesCycle WHEN '春季' THEN AveFreshCocoon ELSE 0 END)SpringAveFreshCocoon,"
                +"SUM(CASE TimesCycle WHEN '夏季' THEN AveFreshCocoon ELSE 0 END)SummerAveFreshCocoon,"
                + "SUM(((CASE TimesCycle WHEN '正秋' THEN AveFreshCocoon ELSE 0 END) * (CASE TimesCycle WHEN '正秋' THEN FreshCocoonYield ELSE 0 END))+((CASE TimesCycle WHEN '晚秋' THEN AveFreshCocoon ELSE 0 END)*(CASE TimesCycle WHEN '晚秋' THEN FreshCocoonYield ELSE 0 END)))FallAveFreshCocoon,"
                + "SUM(CASE TimesCycle WHEN '春季' THEN SmallRearSilk ELSE 0 END)SpringSmallRearSilk,"
                +"SUM(CASE TimesCycle WHEN '夏季' THEN SmallRearSilk ELSE 0 END)SummerSmallRearSilk,"
                +"SUM((CASE TimesCycle WHEN '正秋' THEN SmallRearSilk ELSE 0 END) + (CASE TimesCycle WHEN '晚秋' THEN SmallRearSilk ELSE 0 END))FallSmallRearSilk,"
                +"SUM(RawSilkYield)RawSilkYield,SUM(RawSilkValue)RawSilkValue,SUM(SilkYield)SilkYield,SUM(SilkValue)SilkValue,"
                +"SUM(SilkQuiltYield)SilkQuiltYield,SUM(SilkQuiltValue)SilkQuiltValue "
                +"from t_report t1 left join T_MulQuartProduc mc on t1.Id = mc.ReportId "
                +"left join T_MulAnnualProducStatist ma on ma.ReportId = t1.Id left join "
                +"T_MulAreaStatist mas on mas.ReportId = t1.Id right join T_Area Area on Area.Code = t1.AreaCode "
                + "where ParentCode!= @AreaCode  and t1.FillInYear = @FillInYear and t1.Status = @ReportStatus group by rollup(Area.ParentCode)) t left join T_Area  area on area.Code = t.ParentCode "
                + "inner join T_Dict Dict  on Dict.GroupCode = 'ReportYear' and DictValue = @FillInYear where area.ParentCode = @AreaCode  or area.Code = @AreaCode order by AreaCode asc";
            #endregion
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    var paramlist = new DynamicParameters();
                    paramlist.Add("AreaCode", model.AreaCode);
                    paramlist.Add("FillInYear", model.FillInYear);
                    paramlist.Add("ReportStatus", model.Status);
                    //paramlist.Add("CultivarCode", model.itemText);
                    List<MulAnnStatsModel> list = new List<MulAnnStatsModel>();
                    List<MulAnnStatsModel> listResult = new List<MulAnnStatsModel>();
                    if (model.AreaCode != DefaultKey.ProvinceCode)//不为省级用户
                    {
                        list = dbCon.Query<MulAnnStatsModel>(StrAreaSql, paramlist).ToList();
                        listResult = Reckon(list, model.AreaCode);
                        //foreach (var item in listResult) {
                        //    if (item.AreaCode == model.AreaCode) {
                        //        item.AreaName = "合计";
                        //    }
                        //}
                        result.model = listResult;
                    }
                    else //省级用户
                    {
                        AreaServices area = new AreaServices();
                        DictServices dict = new DictServices();
                        List<AreaListModel> areaList = area.GetAreaCodeList(model.AreaCode);
                        List<MulAnnStatsModel> listResultTwo = new List<MulAnnStatsModel>();
                        foreach (var item in areaList)
                        {
                            var paramlist2 = new DynamicParameters();
                            paramlist2.Add("AreaCode", item.AreaCode);
                            paramlist2.Add("FillInYear", model.FillInYear);
                            paramlist2.Add("ReportStatus", model.Status);
                            list = dbCon.Query<MulAnnStatsModel>(StrAreaSql, paramlist2).ToList();
                            listResultTwo = Reckon(list, item.AreaCode);
                            if(listResultTwo.Count()>0)
                            {
                                MulAnnStatsModel nowModel = listResultTwo.Where(s => s.AreaCode == item.AreaCode).FirstOrDefault();
                                listResult.Add(nowModel);
                            }
                            result.model = listResult;
                        }
                        MulAnnStatsModel total = new MulAnnStatsModel();
                        total.AreaName = "云南省";
                        total.year = dict.GetPZName(model.FillInYear, "ReportYear");
                        total.IntoMulArea = listResult.Sum(p => p.IntoMulArea);
                        total.SpringSeedSize = listResult.Sum(p => p.SpringSeedSize);
                        total.SummerSeedSize = listResult.Sum(p => p.SummerSeedSize);
                        total.FallSeedSize = listResult.Sum(p => p.FallSeedSize);
                        total.TotalSeedSize = total.SpringSeedSize + total.SummerSeedSize + total.FallSeedSize;
                        total.SpringFreshCocoonYield = listResult.Sum(p => p.SpringFreshCocoonYield);
                        total.SummerFreshCocoonYield = listResult.Sum(p => p.SummerFreshCocoonYield);
                        total.FallFreshCocoonYield = listResult.Sum(p => p.FallFreshCocoonYield);
                        total.TotalFreshCocoonYield = total.SpringFreshCocoonYield + total.SummerFreshCocoonYield + total.FallFreshCocoonYield;
                        total.RawSilkValue = listResult.Sum(p => p.RawSilkValue);
                        total.RawSilkYield = listResult.Sum(p => p.RawSilkYield);
                        total.SilkQuiltValue = listResult.Sum(p => p.SilkQuiltValue);
                        total.SilkQuiltYield = listResult.Sum(p => p.SilkQuiltYield);
                        total.SilkValue = listResult.Sum(p => p.SilkValue);
                        total.SilkYield = listResult.Sum(p => p.SilkYield);
                        total.SpringSmallRearSilk = listResult.Sum(p => p.SpringSmallRearSilk);
                        total.SummerSmallRearSilk = listResult.Sum(p => p.SummerSmallRearSilk);
                        total.FallSmallRearSilk = listResult.Sum(p => p.FallSmallRearSilk);
                        total.TotalSmallRearSilk = total.SpringSmallRearSilk + total.SummerSmallRearSilk + total.FallSmallRearSilk;
                        //均价* 产量的值
                        //春
                        decimal? val1 = 0;
                        //夏
                        decimal? val2 = 0;
                        //秋
                        decimal? val3 = 0;
                        foreach (var ss in listResult)
                        {
                            val1 = val1 + (ss.SpringAveFreshCocoon * ss.SpringFreshCocoonYield);
                            val2 = val2 + (ss.SummerAveFreshCocoon * ss.SummerFreshCocoonYield);
                            val3 = val3 + (ss.FallAveFreshCocoon * ss.FallFreshCocoonYield);
                        }
                        //春的均价
                        if( total.SpringFreshCocoonYield!=null && total.SpringFreshCocoonYield!=decimal.Zero)
                        {
                            total.SpringAveFreshCocoon = val1 / (total.SpringFreshCocoonYield);
                        }
                        if (total.SummerFreshCocoonYield != null && total.SummerFreshCocoonYield != decimal.Zero)
                        {
                            //夏的均价
                            total.SummerAveFreshCocoon = val2 / (total.SummerFreshCocoonYield);
                        }
                        if (total.FallFreshCocoonYield != null && total.FallFreshCocoonYield != decimal.Zero)
                        {
                            //秋的均价
                            total.FallAveFreshCocoon = val3 / (total.FallFreshCocoonYield);
                        }
                        if (total.TotalFreshCocoonYield != null && total.TotalFreshCocoonYield != decimal.Zero)
                        {
                            //总的均价
                            total.TotalAveFreshCocoon = Convert.ToDecimal(Math.Round(Convert.ToDouble((total.SpringFreshCocoonYield * total.SpringAveFreshCocoon + total.SummerFreshCocoonYield * total.SummerAveFreshCocoon + total.FallFreshCocoonYield * total.FallAveFreshCocoon) / total.TotalFreshCocoonYield), 2));

                        }
                        List<MulAnnStatsModel> listResult2 = new List<MulAnnStatsModel>();
                        listResult2.Add(total);
                        for (var i = 0; i < listResult.Count(); i++)
                        {
                            listResult2.Add(listResult[i]);
                        }

                        result.model = listResult2;
                    }
                        
                        if (list != null)
                        {

                            result.status = DefaultKey.SuccessResult;
                            result.message = "获取全年蚕桑生产情况统计数据成功！";
                        }
                        else
                        {
                            result.status = DefaultKey.InfoResult;
                            result.message = "获取全年蚕桑生产情况统计数据失败！";
                        }
                        dbCon.Dispose();//数据库连接释放
                    }
                }
            catch (Exception ex)
            {
                result.status = DefaultKey.ErrorResult;
                result.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取全年蚕桑生产情况统计数据失败", ex);
            }
            return result;
        }
        /// <summary>
        /// 获取时间趋势图数据列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResResult<List<MulAnnStatsModel>> GetStatsByTimeList(MulNowModel model)
        {
            DictServices dictServices = new DictServices();
            List<Select> listDict = dictServices.GetListByGroupCode("ReportYear").model;
            ResResult<List<MulAnnStatsModel>> result = new ResResult<List<MulAnnStatsModel>>();
            List<MulAnnStatsModel> list = new List<MulAnnStatsModel>();
            if (listDict.Count > 0)
            {
                if (!string.IsNullOrEmpty(model.AreaCode))
                {
                    for (var i = 0; i < listDict.Count; i++)
                    {
                        model.FillInYear = listDict[i].label;
                        List<MulAnnStatsModel> listOne = GetStatsList(model).model;
                        if (listOne.Count > 0)
                        {
                            list.Add(listOne[0]);
                        }
                    }
                    if (list.Count > 0)
                    {
                        result.message = "时间趋势数据列表获取成功";
                        result.model = list.OrderBy(s => s.year).ToList();
                        result.status = DefaultKey.SuccessResult;
                    }
                    else
                    {
                        result.message = "时间趋势数据列表为空";
                        result.status = DefaultKey.InfoResult;
                    }
                }
                else
                {
                    result.message = "时间趋势数据列表失败,当前地区不存在";
                    result.status = DefaultKey.ErrorResult;
                }
            }
            else
            {
                result.message = "时间趋势数据列表失败,时间为空";
                result.status = DefaultKey.ErrorResult;
            }
            return result;
        }
        public ResResult<List<MulAnnStatsModel>> GetDetailStats(MulNowModel model)
        {
            ResResult<List<MulAnnStatsModel>> res = new ResResult<List<MulAnnStatsModel>>();
            List<MulAnnStatsModel> list = new List<MulAnnStatsModel>();
            string ProvinceCode = DefaultKey.ProvinceCode;
            string FillInYear = model.FillInYear;
            try
            {
                if (model.AreaCode == ProvinceCode)
                {
                    var First = GetStatsList(model).model;
                    //First[0].AreaName = "合计";
                    //list.Add(First[0]);//省级汇总合计
                    foreach (var item in First)
                    {
                        if (item.AreaCode != ProvinceCode)
                        {
                            MulNowModel model2 = new MulNowModel();
                            model2.FillInYear = FillInYear;
                            model2.AreaCode = item.AreaCode;
                            model2.Status = model.Status;
                            
                            var Second = GetStatsList(model2).model;
                            list.Add(item);
                            foreach (var item2 in Second)
                            {
                                if (model2.AreaCode != item2.AreaCode)
                                {
                                    list.Add(item2);
                                }
                            }
                        }
                    }
                }
                else
                {
                    list = GetStatsList(model).model;//数据获取
                }
            }
            catch (Exception ex)
            {
                res.model = list;
                res.message = "获取统计数据详情失败" + ex.ToString().Trim();
                res.status = DefaultKey.SuccessResult;
            }
            if (list != null)
            {
                res.model = list;
                res.message = "获取统计数据详情成功";
                res.status = DefaultKey.SuccessResult;
            }
            else
            {
                res.message = "获取统计数据详情成失败";
                res.status = DefaultKey.ErrorResult;
            }
            return res;
        }

        /// <summary>
        /// 填报时获取春夏秋数据   并计算增减
        /// </summary>
        /// <param name="id"></param>
        /// <param name="AreaCode"></param>
        /// <param name="FillInYear"></param>
        /// <returns></returns>
        public List<MulAnnualProducStatistModel> GetDataList(string id, string AreaCode, string FillInYear) {
            DictServices dict = new DictServices();
            //今年
            string nowYear = dict.GetKey(FillInYear);
            //去年
            string lastyear = (Convert.ToInt32(nowYear) - 1).ToString();
            //去年报表id
            string lastReportId = rs.GetReportId("MulAnnualProducStatist",AreaCode, lastyear);
            List<MulAnnualProducStatistModel> list = new List<MulAnnualProducStatistModel>();
            //今年数据
            MulAnnualProducStatistModel nowData = GetModel(AreaCode,id, nowYear).model;
            if (nowData!=null) {
                nowData.Name = FillInYear;
                nowData.SeedSizeTotal = nowData.SeedSizeSpring + nowData.SeedSizeSummer + nowData.SeedSizeAutumn;
                nowData.FCYTotal = nowData.FCYSpring + nowData.FCYSummer + nowData.FCYAutumn;
                if (nowData.FCYTotal == 0 || nowData.FCYTotal == null)
                {
                    nowData.AFCTotal = 0;
                }
                else
                {
                    nowData.AFCTotal = ((nowData.AFCSpring * nowData.FCYSpring) + (nowData.AFCSummer * nowData.FCYSummer) + (nowData.AFCAutumn * nowData.FCYAutumn)) / nowData.FCYTotal;
                }
                nowData.SRSTotal = nowData.SRSSpring + nowData.SRSSummer + nowData.SRSAutumn;
            }else
            {
                nowData = new MulAnnualProducStatistModel();
                nowData.Name = FillInYear;
                nowData.SeedSizeTotal = 0;
                nowData.SeedSizeSpring = 0;
                nowData.SeedSizeSummer = 0;
                nowData.SeedSizeAutumn = 0;
                nowData.FCYTotal = 0;
                nowData.FCYSpring = 0;
                nowData.FCYSummer = 0;
                nowData.FCYAutumn = 0;
                nowData.AFCTotal = 0;
                nowData.AFCSpring = 0;
                nowData.AFCSummer = 0;
                nowData.AFCAutumn = 0;
                nowData.SRSTotal = 0;
                nowData.SRSSpring = 0;
                nowData.SRSSummer = 0;
                nowData.SRSAutumn = 0;
                nowData.IntoMulArea = 0;
                nowData.RawSilkValue = 0;
                nowData.RawSilkYield = 0;
                nowData.SilkQuiltValue = 0;
                nowData.SilkQuiltYield = 0;
                nowData.SilkValue = 0;
                nowData.SilkYield = 0;
            }
            //去年数据
            MulAnnualProducStatistModel lastData = GetModel(AreaCode, lastReportId, lastyear).model;
            if (lastData!=null) {
                lastData.Name = "上一年";
                lastData.SeedSizeTotal = lastData.SeedSizeSpring + lastData.SeedSizeSummer + lastData.SeedSizeAutumn;
                lastData.FCYTotal = lastData.FCYSpring + lastData.FCYSummer + lastData.FCYAutumn;
                if (lastData.FCYTotal == 0 || lastData.FCYTotal == null)
                {
                    lastData.AFCTotal = 0;
                }
                else
                {
                    lastData.AFCTotal = ((lastData.AFCSpring * lastData.FCYSpring) + (lastData.AFCSummer * lastData.FCYSummer) + (lastData.AFCAutumn / 2 * lastData.FCYAutumn)) / lastData.FCYTotal;
                }
                lastData.SRSTotal = lastData.SRSSpring + lastData.SRSSummer + lastData.SRSAutumn;
            }else
            {
                lastData = new MulAnnualProducStatistModel();
                lastData.Name = "上一年";
                lastData.SeedSizeTotal = 0;
                lastData.SeedSizeSpring = 0;
                lastData.SeedSizeSummer = 0;
                lastData.SeedSizeAutumn = 0;
                lastData.FCYTotal = 0;
                lastData.FCYSpring = 0;
                lastData.FCYSummer = 0;
                lastData.FCYAutumn = 0;
                lastData.AFCTotal = 0;
                lastData.AFCSpring = 0;
                lastData.AFCSummer = 0;
                lastData.AFCAutumn = 0;
                lastData.SRSTotal = 0;
                lastData.SRSSpring = 0;
                lastData.SRSSummer = 0;
                lastData.SRSAutumn = 0;
                lastData.IntoMulArea = 0;
                lastData.RawSilkValue = 0;
                lastData.RawSilkYield = 0;
                lastData.SilkQuiltValue = 0;
                lastData.SilkQuiltYield = 0;
                lastData.SilkValue = 0;
                lastData.SilkYield = 0;
            }
            //增减数
            MulAnnualProducStatistModel modelone = new MulAnnualProducStatistModel();
            modelone.Name = "增减数";
            modelone.IntoMulArea = nowData.IntoMulArea - lastData.IntoMulArea;
            modelone.SeedSizeSpring = nowData.SeedSizeSpring - lastData.SeedSizeSpring;
            modelone.SeedSizeSummer = nowData.SeedSizeSummer - lastData.SeedSizeSummer;
            modelone.SeedSizeAutumn = nowData.SeedSizeAutumn - lastData.SeedSizeAutumn;
            modelone.SeedSizeTotal = modelone.SeedSizeSpring+ modelone.SeedSizeSummer+ modelone.SeedSizeAutumn;

            modelone.FCYSpring = nowData.FCYSpring - lastData.FCYSpring;
            modelone.FCYSummer = nowData.FCYSummer - lastData.FCYSummer;
            modelone.FCYAutumn = nowData.FCYAutumn - lastData.FCYAutumn;
            modelone.FCYTotal = modelone.FCYSpring + modelone.FCYSummer + modelone.FCYAutumn;

            modelone.AFCSpring = nowData.AFCSpring - lastData.AFCSpring;
            modelone.AFCSummer = nowData.AFCSummer - lastData.AFCSummer;
            modelone.AFCAutumn = nowData.AFCAutumn - lastData.AFCAutumn;
            modelone.AFCTotal = nowData.AFCTotal-lastData.AFCTotal;

            modelone.SRSSpring = nowData.SRSSpring - lastData.SRSSpring;
            modelone.SRSSummer = nowData.SRSSummer - lastData.SRSSummer;
            modelone.SRSAutumn = nowData.SRSAutumn - lastData.SRSAutumn;
            modelone.SRSTotal = modelone.SRSSpring + modelone.SRSSummer + modelone.SRSAutumn;

            modelone.RawSilkYield = nowData.RawSilkYield - lastData.RawSilkYield;
            modelone.RawSilkValue = nowData.RawSilkValue - lastData.RawSilkValue;
            modelone.SilkYield = nowData.SilkYield - lastData.SilkYield;
            modelone.SilkValue = nowData.SilkValue - lastData.SilkValue;
            modelone.SilkQuiltYield = nowData.SilkQuiltYield - lastData.SilkQuiltYield;
            modelone.SilkQuiltValue = nowData.SilkQuiltValue - lastData.SilkQuiltValue;
            //增减％
            MulAnnualProducStatistModel modeltwo = new MulAnnualProducStatistModel();
            modeltwo.Name = "增减％";
            if (lastData.IntoMulArea==0|| lastData.IntoMulArea==null) {
                modeltwo.IntoMulArea = 0;
            }else
            {
                modeltwo.IntoMulArea = modelone.IntoMulArea / lastData.IntoMulArea;
            }
            if (lastData.SeedSizeSpring==0|| lastData.SeedSizeSpring==null) {
                modeltwo.SeedSizeSpring = 0;
            }
            else
            {
                modeltwo.SeedSizeSpring = modelone.SeedSizeSpring / lastData.SeedSizeSpring;
            }
            if (lastData.SeedSizeSummer==0|| lastData.SeedSizeSummer==null) {
                modeltwo.SeedSizeSummer = 0;
            }
            else
            {
                modeltwo.SeedSizeSummer = modelone.SeedSizeSummer / lastData.SeedSizeSummer;
            }
            if (lastData.SeedSizeAutumn==0|| lastData.SeedSizeAutumn==null) {
                modeltwo.SeedSizeAutumn = 0;
            }
            else
            {
                modeltwo.SeedSizeAutumn = modelone.SeedSizeAutumn / lastData.SeedSizeAutumn;
            }
            modeltwo.SeedSizeTotal = modeltwo.SeedSizeSpring + modeltwo.SeedSizeSummer + modeltwo.SeedSizeAutumn;

            if (lastData.FCYSpring == 0|| lastData.FCYSpring==null) {
                modeltwo.FCYSpring = 0;
            }
            else
            {
                modeltwo.FCYSpring = modelone.FCYSpring / lastData.FCYSpring;
            }
            if (lastData.FCYSummer==0|| lastData.FCYSummer==null) {
                modeltwo.FCYSummer = 0;
            }
            else
            {
                modeltwo.FCYSummer = modelone.FCYSummer / lastData.FCYSummer;
            }
            if (lastData.FCYAutumn==0|| lastData.FCYAutumn==null) {
                modeltwo.FCYAutumn = 0;
            }
            else
            {
                modeltwo.FCYAutumn = modelone.FCYAutumn / lastData.FCYAutumn;
            }
            modeltwo.FCYTotal = modeltwo.FCYSpring + modeltwo.FCYSummer + modeltwo.FCYAutumn;

            if (lastData.AFCSpring==0|| lastData.AFCSpring==null) {
                modeltwo.AFCSpring = 0;
            }
            else
            {
                modeltwo.AFCSpring = modelone.AFCSpring / lastData.AFCSpring;
            }
            if (lastData.AFCSummer==0|| lastData.AFCSummer==null) {
                modeltwo.AFCSummer = 0;
            }
            else
            {
                modeltwo.AFCSummer = modelone.AFCSummer / lastData.AFCSummer;
            }
            if (lastData.AFCAutumn==0|| lastData.AFCAutumn==null) {
                modeltwo.AFCAutumn = 0;
            }else
            {
                modeltwo.AFCAutumn = modelone.AFCAutumn / lastData.AFCAutumn;
            }
            modeltwo.AFCTotal = modeltwo.AFCSpring + modeltwo.AFCSummer + modeltwo.AFCAutumn;

            if (lastData.SRSSpring==0|| lastData.SRSSpring==null) {
                modeltwo.SRSSpring = 0;
            }
            else
            {
                modeltwo.SRSSpring = modelone.SRSSpring / lastData.SRSSpring;
            }
            if (lastData.SRSSummer==0|| lastData.SRSSummer==null) {
                modeltwo.SRSSummer = 0;
            }
            else
            {
                modeltwo.SRSSummer = modelone.SRSSummer / lastData.SRSSummer;
            }
            if (lastData.SRSAutumn==0|| lastData.SRSAutumn==null) {
                modeltwo.SRSAutumn = 0;
            }
            else
            {
                modeltwo.SRSAutumn = modelone.SRSAutumn / lastData.SRSAutumn;
            }
            modeltwo.SRSTotal = modeltwo.SRSSpring + modeltwo.SRSSummer + modeltwo.SRSAutumn;

            if (lastData.RawSilkYield==0|| lastData.RawSilkYield==null) {
                modeltwo.RawSilkYield = 0;
            }
            else
            {
                modeltwo.RawSilkYield = modelone.RawSilkYield / lastData.RawSilkYield;
            }
            if (lastData.RawSilkValue == 0 || lastData.RawSilkValue == null)
            {
                modeltwo.RawSilkValue = 0;
            }
            else
            {
                modeltwo.RawSilkValue = modelone.RawSilkValue / lastData.RawSilkValue;
            }
            if (lastData.SilkYield == 0 || lastData.SilkYield == null)
            {
                modeltwo.SilkYield = 0;
            }
            else
            {
                modeltwo.SilkYield = modelone.SilkYield / lastData.SilkYield;
            }
            if (lastData.SilkValue == 0 || lastData.SilkValue == null)
            {
                modeltwo.SilkValue = 0;
            }
            else
            {
                modeltwo.SilkValue = modelone.SilkValue / lastData.SilkValue;
            }
            if (lastData.SilkQuiltYield == 0 || lastData.SilkQuiltYield == null)
            {
                modeltwo.SilkQuiltYield = 0;
            }
            else
            {
                modeltwo.SilkQuiltYield = modelone.SilkQuiltYield / lastData.SilkQuiltYield;
            }
            if (lastData.SilkQuiltValue == 0 || lastData.SilkQuiltValue == null)
            {
                modeltwo.SilkQuiltValue = 0;
            }
            else
            {
                modeltwo.SilkQuiltValue = modelone.SilkQuiltValue / lastData.SilkQuiltValue;
            }
            list.Add(nowData);
            list.Add(lastData);
            list.Add(modelone);
            list.Add(modeltwo);
            return list;
        }

        /// <summary>
        /// 重新计算当前地区的春、夏、秋均价
        /// </summary>
        /// <param name="list"></param>
        /// <param name="AreaCode"></param>
        /// <returns></returns>
        public List<MulAnnStatsModel> Reckon(List<MulAnnStatsModel> list, string AreaCode)
        {
            List<MulAnnStatsModel> listResult = new List<MulAnnStatsModel>();
            //除去合计外的所有数据集
            var leaveList = list.Where(s => s.AreaCode != AreaCode).ToList();
            foreach (var MulAnnualRear in list)
            {

                MulAnnualRear.TotalFreshCocoonYield = MulAnnualRear.SpringFreshCocoonYield + MulAnnualRear.SummerFreshCocoonYield + MulAnnualRear.FallFreshCocoonYield;
                MulAnnualRear.TotalSeedSize = MulAnnualRear.SpringSeedSize + MulAnnualRear.SummerSeedSize + MulAnnualRear.FallSeedSize;
                MulAnnualRear.TotalSmallRearSilk = MulAnnualRear.SpringSmallRearSilk + MulAnnualRear.SummerSmallRearSilk + MulAnnualRear.FallSmallRearSilk;
                if (MulAnnualRear.FallFreshCocoonYield == 0)
                {
                    MulAnnualRear.FallAveFreshCocoon = 0;
                }
                else
                {
                    MulAnnualRear.FallAveFreshCocoon = MulAnnualRear.FallAveFreshCocoon / MulAnnualRear.FallFreshCocoonYield;
                }
                //鲜茧均价
                if (MulAnnualRear.TotalFreshCocoonYield > decimal.Zero)
                {
                    MulAnnualRear.TotalAveFreshCocoon = Convert.ToDecimal(Math.Round(Convert.ToDouble((MulAnnualRear.SpringFreshCocoonYield * MulAnnualRear.SpringAveFreshCocoon + MulAnnualRear.SummerFreshCocoonYield * MulAnnualRear.SummerAveFreshCocoon + MulAnnualRear.FallFreshCocoonYield * MulAnnualRear.FallAveFreshCocoon) / MulAnnualRear.TotalFreshCocoonYield), 2));

                }
                else
                {
                    MulAnnualRear.TotalAveFreshCocoon = 0;
                }
                //合计时重新计算春、夏、秋均价
                if (MulAnnualRear.AreaCode == AreaCode)
                {
                    //均价*产量的值
                    //春
                    decimal? val1 = 0;
                    //夏
                    decimal? val2 = 0;
                    foreach (var item in leaveList)
                    {
                        val1 = val1 + (item.SpringAveFreshCocoon * item.SpringFreshCocoonYield);
                        val2 = val2 + (item.SummerAveFreshCocoon * item.SummerFreshCocoonYield);
                    }
                    //春的均价
                    if (MulAnnualRear.SpringFreshCocoonYield==0) {
                        MulAnnualRear.SpringAveFreshCocoon = 0;
                    }else
                    {
                        MulAnnualRear.SpringAveFreshCocoon = val1 / (MulAnnualRear.SpringFreshCocoonYield);
                    }
                    //夏的均价
                    if (MulAnnualRear.SummerFreshCocoonYield==0) {
                        MulAnnualRear.SummerAveFreshCocoon = 0;
                    }else
                    {
                        MulAnnualRear.SummerAveFreshCocoon = val2 / (MulAnnualRear.SummerFreshCocoonYield);
                    }
                    //合计的均价
                    if (MulAnnualRear.TotalFreshCocoonYield==0) {
                        MulAnnualRear.TotalAveFreshCocoon = 0;
                    }else
                    {
                        MulAnnualRear.TotalAveFreshCocoon = Convert.ToDecimal(Math.Round(Convert.ToDouble((MulAnnualRear.SpringFreshCocoonYield * MulAnnualRear.SpringAveFreshCocoon + MulAnnualRear.SummerFreshCocoonYield * MulAnnualRear.SummerAveFreshCocoon + MulAnnualRear.FallFreshCocoonYield * MulAnnualRear.FallAveFreshCocoon) / MulAnnualRear.TotalFreshCocoonYield), 2));
                    }
                    
                }
                listResult.Add(MulAnnualRear);
            }
            return listResult;
        }
        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public MemoryStream Method(MulNowModel model)
        {
            string ReportId = rs.GetReportId(model.ReportCode, model.AreaCode, model.FillInYear);
            FileStream file = null;
            //创建下载Excel文件
            var workbook = new HSSFWorkbook();
            string fileIndex = HttpRuntime.AppDomainAppPath.ToString();
            string filename = @"tempfile\Basic" + DateTime.Now.Ticks.ToString() + ".xls";//临时文件，定期在tempfile手动删除
            string tempExcel = fileIndex + filename;//临时文件路径及全称
            string templateName = fileIndex + @"Excel\Excel.xls";//模板文件
            file = new FileStream(templateName, FileMode.Open, FileAccess.Read);//读模板文件
            HSSFWorkbook templateBook = new HSSFWorkbook(file);//读取模板文件信息转换成Excel工作薄
            HSSFSheet sheet = templateBook.GetSheet("MulAnnualProducStatist") as HSSFSheet;//读取模板所在sheet位置
            //ResResult<MulAnnualProducStatistModel> pagelist = mulAnnualProducStatistServices.GetModel(model.AreaCode, ReportId, model.FillInYear);//数据获取
            //ResResult<MulAnnViewModel> pagelistTwo = mulAnnualProducStatistServices.GetModel(model);//数据获取
            List<MulAnnualProducStatistModel> listModel = GetDataList(model.Id, model.AreaCode, model.FillInYear);

            HSSFRow row = (HSSFRow)sheet.GetRow(1);//读取模板第二行
            var style = row.GetCell(0).CellStyle;//读取模板第二行第一单元格样式并保存
            row.GetCell(0).CellStyle = style;//对第二行第一单元格设置原来样式

            for (var i = 0; i < listModel.Count; i++)
            {
                HSSFRow datarow = (HSSFRow)sheet.CreateRow(i + 3);
                datarow.CreateCell(0).SetCellValue(listModel[i].Name == null ? "" : listModel[i].Name.ToString());
                datarow.CreateCell(1).SetCellValue(listModel[i].IntoMulArea == null ? "" : Math.Round((double)listModel[i].IntoMulArea, 2).ToString());
                datarow.CreateCell(2).SetCellValue(listModel[i].SeedSizeTotal == null ? "" : Math.Round((double)listModel[i].SeedSizeTotal, 2).ToString());
                datarow.CreateCell(3).SetCellValue(listModel[i].SeedSizeSpring == null ? "" : Math.Round((double)listModel[i].SeedSizeSpring, 2).ToString());
                datarow.CreateCell(4).SetCellValue(listModel[i].SeedSizeSummer == null ? "" : Math.Round((double)listModel[i].SeedSizeSummer, 2).ToString());
                datarow.CreateCell(5).SetCellValue(listModel[i].SeedSizeAutumn == null ? "" : Math.Round((double)listModel[i].SeedSizeAutumn, 2).ToString());
                datarow.CreateCell(6).SetCellValue(listModel[i].FCYTotal == null ? "" : Math.Round((double)listModel[i].FCYTotal, 2).ToString());
                datarow.CreateCell(7).SetCellValue(listModel[i].FCYSpring == null ? "" : Math.Round((double)listModel[i].FCYSpring, 2).ToString());
                datarow.CreateCell(8).SetCellValue(listModel[i].FCYSummer == null ? "" : Math.Round((double)listModel[i].FCYSummer, 2).ToString());
                datarow.CreateCell(9).SetCellValue(listModel[i].FCYAutumn == null ? "" : Math.Round((double)listModel[i].FCYAutumn, 2).ToString());
                datarow.CreateCell(10).SetCellValue(listModel[i].AFCTotal == null ? "" : Math.Round((double)listModel[i].AFCTotal, 2).ToString());
                datarow.CreateCell(11).SetCellValue(listModel[i].AFCSpring == null ? "" : Math.Round((double)listModel[i].AFCSpring, 2).ToString());
                datarow.CreateCell(12).SetCellValue(listModel[i].AFCSummer == null ? "" : Math.Round((double)listModel[i].AFCSummer, 2).ToString());
                datarow.CreateCell(13).SetCellValue(listModel[i].AFCAutumn == null ? "" : Math.Round((double)listModel[i].AFCAutumn, 2).ToString());
                datarow.CreateCell(14).SetCellValue(listModel[i].SRSTotal == null ? "" : Math.Round((double)listModel[i].SRSTotal, 2).ToString());
                datarow.CreateCell(15).SetCellValue(listModel[i].SRSSpring == null ? "" : Math.Round((double)listModel[i].SRSSpring, 2).ToString());
                datarow.CreateCell(16).SetCellValue(listModel[i].SRSSummer == null ? "" : Math.Round((double)listModel[i].SRSSummer, 2).ToString());
                datarow.CreateCell(17).SetCellValue(listModel[i].SRSAutumn == null ? "" : Math.Round((double)listModel[i].SRSAutumn, 2).ToString());
                datarow.CreateCell(18).SetCellValue(listModel[i].RawSilkYield == null ? "" : Math.Round((double)listModel[i].RawSilkYield, 2).ToString());
                datarow.CreateCell(19).SetCellValue(listModel[i].RawSilkValue == null ? "" : Math.Round((double)listModel[i].RawSilkValue, 2).ToString());
                datarow.CreateCell(20).SetCellValue(listModel[i].SilkYield == null ? "" : Math.Round((double)listModel[i].SilkYield, 2).ToString());
                datarow.CreateCell(21).SetCellValue(listModel[i].SilkValue == null ? "" : Math.Round((double)listModel[i].SilkValue, 2).ToString());
                datarow.CreateCell(22).SetCellValue(listModel[i].SilkQuiltYield == null ? "" : Math.Round((double)listModel[i].SilkQuiltYield, 2).ToString());
                datarow.CreateCell(23).SetCellValue(listModel[i].SilkQuiltValue == null ? "" : Math.Round((double)listModel[i].SilkQuiltValue, 2).ToString());
                for (int j = 0; j <= 23; j++)
                {
                    datarow.GetCell(j).CellStyle = style;
                }
            }

            sheet.CopyTo(workbook, "Sheet1", true, true);
            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                return ms;
            }
        }

        /// <summary>
        /// 导出统计数据
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <summary>
        /// 导出统计数据
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public MemoryStream MethodStats(MulNowModel model)
        {
            string FillInYear = model.FillInYear;
            AreaServices areaServices = new AreaServices();
            //创建下载Excel文件
            var workbook = new HSSFWorkbook();
            string AreaName = areaServices.GetByCodeModel(model.AreaCode).model.Name == "云南省" ? "云南省_县(市、州)" : areaServices.GetByCodeModel(model.AreaCode).model.Name;//获取当前行政区划名称
            getExcelStatsList(workbook, GetDetailStats(model).model, AreaName);//省级汇总(针对曲线)
            if (model.AreaCode == DefaultKey.ProvinceCode)//判断当前用户是否为省级用户
            {
                AreaName = areaServices.GetByCodeModel(model.AreaCode).model.Name;
                model.FillInYear = FillInYear;
                getExcelStatsList(workbook, GetStatsList(model).model, AreaName + "_州市");//省级汇总(针对州市)
                var areaList = areaServices.GetPrefectureList();//获取州市级列表
                foreach (var areaCode in areaList)
                {
                    model.AreaCode = areaCode;
                    model.FillInYear = FillInYear;
                    getExcelStatsList(workbook, GetDetailStats(model).model, areaServices.GetByCodeModel(areaCode).model.Name);
                }
            }
            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                return ms;
            }
        }
        /// <summary>
        /// 返回excel工作簿
        /// </summary>
        /// <param name="workbook">工作簿</param>
        /// <param name="sheet">子表</param>
        /// <param name="model"></param>
        /// <param name="AreaName">地区名称</param>
        /// <returns></returns>
        public HSSFWorkbook getExcelStatsList(HSSFWorkbook workbook, List<MulAnnStatsModel> list, string AreaName)
        {
            FileStream file = null;
            string fileIndex = HttpRuntime.AppDomainAppPath.ToString();
            string filename = @"tempfile\Basic" + DateTime.Now.Ticks.ToString() + ".xls";//临时文件，定期在tempfile手动删除
            string tempExcel = fileIndex + filename;//临时文件路径及全称
            string templateName = fileIndex + @"Excel\ExcelStats.xls";//模板文件
            file = new FileStream(templateName, FileMode.Open, FileAccess.Read);//读模板文件
            HSSFWorkbook templateBook = new HSSFWorkbook(file);//读取模板文件信息转换成Excel工作薄
            HSSFSheet sheet = templateBook.GetSheet("MulAnnualProducStatist") as HSSFSheet;//读取模板所在sheet位置
            IDataFormat format = templateBook.CreateDataFormat();//创建格式化 实例对象
            //List<VegBasicInfoStatsModel> list = new List<VegBasicInfoStatsModel>();
            //list = null;
            //list = GetDetailStats(model).model;
            #region 县市级表格样式
            HSSFRow row = (HSSFRow)sheet.GetRow(1);//读取模板第二行
            var style = row.GetCell(0).CellStyle;//读取模板第二行第一单元格样式并保存
            #endregion
            //row.GetCell(0).CellStyle = style;//对第二行第一单元格设置原来样式
            #region 州市级表格样式
            //var style2 = row.GetCell(1).CellStyle;
            var styleCityOne = Common.GetOneCityCellStyle(templateBook, style);
            var styleCityInt = Common.GetIntCityCellStyle(templateBook, style);
            var styleAreaOne = Common.GetOneAreaCellStyle(templateBook, style);
            var styleAreaInt = Common.GetIntAreaCellStyle(templateBook, style);
            #endregion
            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    HSSFRow datarow = (HSSFRow)sheet.CreateRow(i + 3);
                    datarow.CreateCell(0).SetCellValue(list[i].AreaName == null ? "" : list[i].AreaName.ToString());
                    datarow.CreateCell(1).SetCellValue(list[i].IntoMulArea == null ? 0.0 : Convert.ToDouble(list[i].IntoMulArea));
                    datarow.CreateCell(2).SetCellValue(list[i].TotalSeedSize == null ? 0 : Convert.ToDouble(list[i].TotalSeedSize));
                    datarow.CreateCell(3).SetCellValue(list[i].SpringSeedSize == null ? 0 : Convert.ToDouble(list[i].SpringSeedSize));
                    datarow.CreateCell(4).SetCellValue(list[i].SummerSeedSize == null ? 0 : Convert.ToDouble(list[i].SummerSeedSize));
                    datarow.CreateCell(5).SetCellValue(list[i].FallSeedSize == null ? 0 : Convert.ToDouble(list[i].FallSeedSize));
                    datarow.CreateCell(6).SetCellValue(list[i].TotalFreshCocoonYield == null ? 0.0 : Convert.ToDouble(list[i].TotalFreshCocoonYield));
                    datarow.CreateCell(7).SetCellValue(list[i].SpringFreshCocoonYield == null ? 0.0 : Convert.ToDouble(list[i].SpringFreshCocoonYield));
                    datarow.CreateCell(8).SetCellValue(list[i].SummerFreshCocoonYield == null ? 0.0 : Convert.ToDouble(list[i].SummerFreshCocoonYield));
                    datarow.CreateCell(9).SetCellValue(list[i].FallFreshCocoonYield == null ? 0.0 : Convert.ToDouble(list[i].FallFreshCocoonYield));
                    datarow.CreateCell(10).SetCellValue(list[i].TotalAveFreshCocoon == null ? 0.0 : Convert.ToDouble(list[i].TotalAveFreshCocoon));
                    datarow.CreateCell(11).SetCellValue(list[i].SpringAveFreshCocoon == null ? 0.0 : Convert.ToDouble(list[i].SpringAveFreshCocoon));
                    datarow.CreateCell(12).SetCellValue(list[i].SummerAveFreshCocoon == null ? 0.0 : Convert.ToDouble(list[i].SummerAveFreshCocoon));
                    datarow.CreateCell(13).SetCellValue(list[i].FallAveFreshCocoon == null ? 0.0 : Convert.ToDouble(list[i].FallAveFreshCocoon));
                    datarow.CreateCell(14).SetCellValue(list[i].TotalSmallRearSilk == null ? 0.0 : Convert.ToDouble(list[i].TotalSmallRearSilk));
                    datarow.CreateCell(15).SetCellValue(list[i].SpringSmallRearSilk == null ? 0.0 : Convert.ToDouble(list[i].SpringSmallRearSilk));
                    datarow.CreateCell(16).SetCellValue(list[i].SummerSmallRearSilk == null ? 0.0 : Convert.ToDouble(list[i].SummerSmallRearSilk));
                    datarow.CreateCell(17).SetCellValue(list[i].FallSmallRearSilk == null ? 0.0 : Convert.ToDouble(list[i].FallSmallRearSilk));
                    datarow.CreateCell(18).SetCellValue(list[i].RawSilkValue == null ? 0.0 : Convert.ToDouble(list[i].RawSilkValue));
                    datarow.CreateCell(19).SetCellValue(list[i].RawSilkYield == null ? 0.0 : Convert.ToDouble(list[i].RawSilkYield));
                    datarow.CreateCell(20).SetCellValue(list[i].SilkYield == null ? 0.0 : Convert.ToDouble(list[i].SilkYield));
                    datarow.CreateCell(21).SetCellValue(list[i].SilkValue == null ? 0.0 : Convert.ToDouble(list[i].SilkValue));
                    datarow.CreateCell(22).SetCellValue(list[i].SilkQuiltYield == null ? 0.0 : Convert.ToDouble(list[i].SilkQuiltYield));
                    datarow.CreateCell(23).SetCellValue(list[i].SilkQuiltValue == null ? 0.0 : Convert.ToDouble(list[i].SilkQuiltValue));
                    if (CustomFun.IsCity(list[i].AreaCode))
                    {//省级和州市级
                        for (int j = 0; j < 24; j++)
                        {
                            datarow.GetCell(j).CellStyle = styleCityOne;
                            if (j == 2 || j == 3 || j == 4 || j == 5)
                            {
                                datarow.GetCell(j).CellStyle = styleCityInt;
                            }
                        }
                    }
                    else//县级
                    {
                        for (int j = 0; j < 24; j++)
                        {
                            datarow.GetCell(j).CellStyle = styleAreaOne;
                            if (j == 2 || j == 3 || j == 4 || j == 5)
                            {
                                datarow.GetCell(j).CellStyle = styleAreaInt;
                            }
                        }
                    }
                }
                sheet.CopyTo(workbook, AreaName, true, true);
            }
            return workbook;
        }
    }
}